<?php
// +----------------------------------------------------------------------
// | my
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: $this <498944516@qq.com>
// +----------------------------------------------------------------------
namespace app\rob\model;

use think\Model;

class WeRob extends Model
{

   
    /**
     * 获取机器人
     */
    public function index($data=''){
    	
       
       switch ($data['keyword']) {
          case 'turing':
            $reply= $this->turing($data['back']);
            break;
          
          default:
            # code...
            break;
        } 



       return $reply;
      
    }

    // 图灵机器人
    // TODO :: 每个企业都可以去配置自己的图灵
    public function turing($keyword="") {
       
        // $settings = get_addon_settings('IdouChat');
        $settings['api_url'] = 'http://www.tuling123.com/openapi/api';
        $settings['api_key'] = '9df6c5ea2698473f943d9a7a18d148d4';
        $api_url = $settings['api_url'] . "?key=" . $settings['api_key'] . "&info=" . $keyword;
         
        $result = file_get_contents ( $api_url );
        $result = json_decode ( $result, true );
        
       
        if ($result ['code'] > 40000 && $result['code'] < 40008) {
            if ($result ['code'] < 40008 && ! empty ( $result ['text'] )) {
                $reply['message']= $result ['text'];
                return $reply;
            } else {
                return false;
            }
        }

        switch ($result ['code']) {
            case '100000' :
                $reply['message']=$result['text'];
                return $reply ;
                break;
            case '200000' :
                $text = $result ['text'] . ',<a href="' . $result ['url'] . '">点击进入</a>';
                $reply['message']=$text;
                return $reply ;
               
                break;
            case '301000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['name'],
                            'Description' => $info ['author'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '302000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['article'],
                            'Description' => $info ['source'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '304000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['name'],
                            'Description' => $info ['count'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '305000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['start'] . '--' . $info ['terminal'],
                            'Description' => $info ['starttime'] . '--' . $info ['endtime'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '306000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['flight'] . '--' . $info ['route'],
                            'Description' => $info ['starttime'] . '--' . $info ['endtime'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '307000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['name'],
                            'Description' => $info ['info'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '308000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['name'],
                            'Description' => $info ['info'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '309000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['name'],
                            'Description' => '价格 : ' . $info ['price'] . ' 满意度 : ' . $info ['satisfaction'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '310000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['number'],
                            'Description' => $info ['info'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '311000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['name'],
                            'Description' => '价格 : ' . $info ['price'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            case '312000' :
                foreach ( $result ['list'] as $info ) {
                    $articles [] = array (
                            'Title' => $info ['name'],
                            'Description' => '价格 : ' . $info ['price'],
                            'PicUrl' => $info ['icon'],
                            'Url' => $info ['detailurl'] 
                    );
                }
                $reply['type']='news';
                $reply['message']= $articles;
                return $reply;
                break;
            default :
                if (empty ( $result ['text'] )) {
                    return false;
                } else {
                     $reply['message']=$result ['text'];
                    return  $reply;
                }
        }
        return true;
    }

    
}